package repositorios;

import excessoes.DAOException;
import interfaces.InterfaceLogEventosDAO;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
import entidades.LogEventos;

/**
 *
 * @author Marcelo Gomes Neto
 */
public class LogEventosDAOHibernate implements InterfaceLogEventosDAO 
{
    private Session session;

    public void setSessao(Session session) {
        this.session = session;
    }

    @Override
    public LogEventos carregarEvento(Integer id) throws DAOException {
        return (LogEventos) this.session.get(LogEventos.class, id);
    }

    @Override
    public void salvarEvento(LogEventos logEventos) throws DAOException 
    {
        try {
           this.session.saveOrUpdate(logEventos);
        }
        catch (ConstraintViolationException e) {
            throw new DAOException("Erro de chave duplicada\n" + e.getConstraintName());
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<LogEventos> listarEvento() 
    {
        Criteria criteria = this.session.createCriteria(LogEventos.class);
        criteria.addOrder((Order.desc("idevento")));
        //criteria.setMaxResults(90);
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<LogEventos> listarLogPorUsuario(int idusuario)
    {
        Criteria criteria = session.createCriteria(LogEventos.class);
        criteria.add(Restrictions.eq("idusuario", idusuario));
        criteria.addOrder((Order.desc("idevento")));
        //this.session.clear();
        return criteria.list();
    }
}
